package com.cqu.lpss.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.lpss.pojo.*;
import com.cqu.lpss.pojo.Class;
import com.cqu.lpss.service.impl.ClassImpl;
import com.cqu.lpss.service.impl.ContestantsImpl;
import com.cqu.lpss.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cqu.lpss.pojo.ChartsData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/contestants")
public class ContestantsController {
    @Autowired
    ContestantsImpl contestantsimpl;
    @Autowired
    ClassImpl classImpl;
    @PostMapping("/login")
    public Result login(@RequestBody Contestants contestant){
        log.info("参赛人员登录：{}",contestant);
        Contestants loginuser = contestantsimpl.login(contestant);
        //登录成功生成jwt令牌
        if(loginuser!=null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("userIndex",loginuser.getConIndex());
            claims.put("userId",loginuser.getConId());
            claims.put("userName",loginuser.getConName());
            claims.put("userClass",loginuser.getConClass());
            claims.put("roleName","contestant");
            claims.put("roleId",'5');

            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        //登录失败，返回错误信息
        return Result.erro("用户名或密码错误");
    }
    @PostMapping("/register")
    public Result register(@RequestBody Contestants contestants){
        Contestants con_sameId = contestantsimpl.findById(contestants.getConId());
        if(con_sameId!=null){
            return Result.erro("SAME_CONID");
        }else{

        }
        return Result.success();
    }
    @PostMapping
    public Result list(
            @RequestParam(defaultValue = "1") int currentPage, // 当前页码
            @RequestParam(defaultValue = "10") int pageSize, // 每页显示数量
            String  conId,
            String  conName,
            Integer conLowAge,
            Integer conHighAge,
            Integer conGender,
            String  conEmail,
            String  conPhone,
            Integer conClass
    ){
        log.info("params:{}"+conHighAge);
        PageBean<Contestants> pageBean = contestantsimpl.list(currentPage, pageSize, conId, conName, conLowAge, conHighAge, conGender, conEmail, conPhone, conClass);
        return Result.success(pageBean);
    }
    @GetMapping("/findById")
    public Result findById(@RequestParam String userId){
        Contestants contestants = contestantsimpl.findById(userId);
        if(contestants!=null){
            return  Result.success(contestants);
        }else {
            return  Result.erro("未知错误");
        }
    }
    @PostMapping("/PersonCenter_upload")
    public Result PersonCenter_upload(@RequestParam String newPassword,@RequestParam String phone,@RequestParam String email,@RequestParam String id){
        int ans = contestantsimpl.PersonCenter_upload(newPassword, phone, email, id);
        if(ans!=0){
            return  Result.success();
        }
        else {
            return Result.erro("密码重置失败，请联系管理员");
        }
    }
    @PostMapping("/uploadedcon_groupbyclass")
    public Result uploadedcon_groupbyclass(){
        List<Class> classes = classImpl.listAll();
        List<Map<Integer, Long>> uploadCon_groupByClass = contestantsimpl.UploadCon_GroupByClass();
        List<ChartsData> chartsDataList = new ArrayList<>();
        for(int i=0;i<uploadCon_groupByClass.size();i++){
            Class cls = classes.get(i);
            Integer cls_id = cls.getClassId();
            String cls_name = cls.getClassName();
            Map<Integer, Long> integerIntegerMap = uploadCon_groupByClass.get(cls_id - 1);
            Long cls_count =integerIntegerMap.get("class_count");
            ChartsData chartsData = new ChartsData(cls_count,cls_name);
            chartsDataList.add(chartsData);
        }
        return Result.success(chartsDataList);

    }
    @PostMapping("/passfirstprj_groupByclass")
    public Result passfirstprj_groupByclass(){
        List<Class> classes = classImpl.listAll();
        List<Map<Integer, Long>> passfirstPrj_groupByClass = contestantsimpl.passfirstPrj_groupByClass();
        List<ChartsData> chartsDataList = new ArrayList<>();
        for(int i=0;i<passfirstPrj_groupByClass.size();i++){
            Class cls = classes.get(i);
            Integer cls_id = cls.getClassId();
            String cls_name = cls.getClassName();
            Map<Integer, Long> integerIntegerMap = passfirstPrj_groupByClass.get(cls_id - 1);
            Long cls_count =integerIntegerMap.get("class_count");
            ChartsData chartsData = new ChartsData(cls_count,cls_name);
            chartsDataList.add(chartsData);
        }
        return Result.success(chartsDataList);
    }
    @PostMapping("/totalnum")
    public Result totalnum(){
        int total = contestantsimpl.totalnum();
        return Result.success(total);
    }
}
